# Below is the complete Python code, including the radar_eq function and the plotting section.

import numpy as np
import matplotlib.pyplot as plt
import math

def radar_eq(pt, freq, g, sigma, te, b, nf, loss, range_):
    # Constants
    c = 3.0e+8  # speed of light
    lambda_ = c / freq  # wavelength
    p_peak = 10 * np.log10(pt)  # convert peak power to dB
    lambda_sqdb = 10 * np.log10(lambda_**2)  # compute wavelength square in dB
    sigmadb = 10 * np.log10(sigma)  # convert sigma to dB
    four_pi_cub = 10 * np.log10((4.0 * np.pi)**3)  # (4pi)^3 in dB
    k_db = 10 * np.log10(1.38e-23)  # Boltzman’s constant in dB
    te_db = 10 * np.log10(te)  # noise temp. in dB
    b_db = 10 * np.log10(b)  # bandwidth in dB
    range_pwr4_db = 10 * np.log10(range_**4)  # vector of target range^4 in dB

    # Implement Equation (1.56)
    num = p_peak + g + g + lambda_sqdb + sigmadb
    den = four_pi_cub + k_db + te_db + b_db + nf + loss + range_pwr4_db
    snr = num - den

    return snr

# Parameters
pt = 1.5e+6  # peak power in Watts
freq = 5.6e+9  # radar operating frequency in Hz
g = 45.0  # antenna gain in dB
sigma = 0.1  # radar cross section in m squared
te = 290.0  # effective noise temperature in Kelvins
b = 5.0e+6  # radar operating bandwidth in Hz
nf = 3.0  # noise figure in dB
loss = 6.0  # radar losses in dB
range_ = np.linspace(25e3, 165e3, 1000)  # target range 25 - 165 Km, 1000 points
rangekm = range_ / 1000  # convert range to kilometers

# Calculate SNR for different radar cross sections
snr1 = radar_eq(pt, freq, g, sigma, te, b, nf, loss, range_)
snr2 = radar_eq(pt, freq, g, sigma/10, te, b, nf, loss, range_)
snr3 = radar_eq(pt, freq, g, sigma*10, te, b, nf, loss, range_)

# Plot SNR versus range for different radar cross sections
plt.figure(1)
plt.plot(rangekm, snr3, 'k-', label='σ = 0 dBsm')
plt.plot(rangekm, snr1, 'k--', label='σ = -10 dBsm')
plt.plot(rangekm, snr2, 'k:', label='σ = -20 dBsm')
plt.grid()
plt.legend()
plt.xlabel('Detection range - Km')
plt.ylabel('SNR - dB')
plt.title('SNR versus Range for Different Radar Cross Sections')

# Calculate SNR for different peak powers
snr1 = radar_eq(pt, freq, g, sigma, te, b, nf, loss, range_)
snr2 = radar_eq(pt * 0.4, freq, g, sigma, te, b, nf, loss, range_)
snr3 = radar_eq(pt * 1.8, freq, g, sigma, te, b, nf, loss, range_)

# Plot SNR versus range for different peak powers
plt.figure(2)
plt.plot(rangekm, snr3, 'k-', label='Pt = 2.16 MW')
plt.plot(rangekm, snr1, 'k--', label='Pt = 1.5 MW')
plt.plot(rangekm, snr2, 'k:', label='Pt = 0.6 MW')
plt.grid()
plt.legend()
plt.xlabel('Detection range - Km')
plt.ylabel('SNR - dB')
plt.title('SNR versus Range for Different Peak Powers')

# Show the plots
plt.show()
